﻿using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Audio;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Common.GlobalNPCs
{
    public class WolfNPC : GlobalNPC
    {
        public override bool InstancePerEntity => true;
        public bool isWerewolf = false;
        public bool isTransforming = false;
        public float transformTimer = 0f;
        public int originalType = -1;
        public int werewolfType = -1;
        public override void SetDefaults(NPC npc)
        {
            originalType = NPCID.Wolf;
            werewolfType = NPCID.Werewolf;
            base.SetDefaults(npc);
        }

        public override void AI(NPC npc)
        {
            if (npc.type == originalType)
            {
                if (Main.moonPhase == 0 && !isWerewolf && !isTransforming)
                {
                    StartTransformation(npc);
                }
                if (isTransforming)
                {
                    HandleTransformation(npc);
                }

                base.AI(npc);
            }
        }
        private void StartTransformation(NPC npc)
        {
            isTransforming = true;
            transformTimer = 0f;
            npc.velocity = Vector2.Zero;
            npc.ai[0] = 0f;
            npc.ai[1] = 0f;
        }
        private void HandleTransformation(NPC npc)
        {
            transformTimer += 1f;
            if (transformTimer < 30f)
            {
                if (Main.rand.NextBool(2))
                {
                    int dust = Dust.NewDust(npc.position, npc.width, npc.height, DustID.DungeonBlue);
                    Main.dust[dust].noGravity = true;
                    Main.dust[dust].scale = 1f;
                    Main.dust[dust].color = Color.Blue;
                }
            }
            else if (transformTimer >= 30f && transformTimer < 60f)
            {
                if (transformTimer == 30f)
                {
                    npc.netUpdate = true;
                    SoundEngine.PlaySound(new SoundStyle("HardcoreGame/Assets/Sounds/NPCs/N_1"), npc.position);
                }
                if (Main.rand.NextBool(1))
                {
                    int dust = Dust.NewDust(npc.position, npc.width, npc.height, DustID.DungeonBlue);
                    Main.dust[dust].noGravity = true;
                    Main.dust[dust].scale = 1f;
                    Main.dust[dust].color = Color.Blue;
                }
                if (transformTimer >= 59f)
                {
                    CompleteTransformation(npc);
                }
            }
        }
        private void CompleteTransformation(NPC npc)
        {
            isTransforming = false;
            isWerewolf = true;
            Vector2 position = npc.position;
            Vector2 velocity = npc.velocity;
            float ai0 = npc.ai[0];
            float ai1 = npc.ai[1];
            int target = npc.target;
            if (NPC.NewNPC(npc.GetSource_FromThis(), (int)position.X, (int)position.Y, werewolfType) is int npcID && Main.npc[npcID] is NPC newNPC)
            {
                newNPC.velocity = velocity;
                newNPC.ai[0] = ai0;
                newNPC.ai[1] = ai1;
                newNPC.target = target;
                newNPC.netUpdate = true;
            }
            npc.active = false;
        }
        public override void OnKill(NPC npc)
        {
            isWerewolf = false;
            isTransforming = false;
            base.OnKill(npc);
        }
        public override void ResetEffects(NPC npc)
        {
            if (Main.moonPhase != 0 && isWerewolf)
            {
                isWerewolf = false;
            }
            base.ResetEffects(npc);
        }
    }
}